package com.jxw.cloudpen.web.microsoft.stremspeed;


import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.codec.http.FullHttpRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;

/**
 * 接受登录结果
 * 登录成功切换心跳处理
 * 记录session
 */
@Slf4j
@ChannelHandler.Sharable
@Service("LoginHandler")
public class LoginHandler extends ChannelInboundHandlerAdapter {
    private ApplicationContext appContext;

    public LoginHandler(ApplicationContext appContext){
        this.appContext = appContext;
    }
    /**
     * 业务逻辑处理
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        if (msg instanceof FullHttpRequest) {
            FullHttpRequest fullHttpRequest = (FullHttpRequest) msg;
            log.info("request uri is {} ",fullHttpRequest.uri());
            if("/stream-speech".equals(fullHttpRequest.uri())){
                log.info("langua-Reco is execute");
                //实时语言识别
                ChannelHandler channelHandler = ctx.channel().pipeline().get("webSocketServerHandler");
                if(channelHandler == null){
                    ctx.channel().pipeline().addLast("webSocketServerHandler",new WebSocketServerHandler());
                }
            }else if("/chatgpt".equals(fullHttpRequest.uri())){
                log.info("chatgpt is execute");
//                //chatGpt
//                ChannelHandler channelHandler = ctx.channel().pipeline().get("chatGptSocketServerHandler");
//                if(channelHandler == null){
//                    ctx.channel().pipeline().addLast("chatGptSocketServerHandler",new ChatGptSocketServerHandler(appContext));
//                }
            }
        }
        //放行
        super.channelRead(ctx, msg);
    }


}
